#!/usr/bin/env tclsh

package provide db [lindex {$Revision: 1.0 $} 1]
namespace eval A::db {}

set pairs "int nDay string destination"

proc getSetter {type name} {
return "
  public void set$name\($type $name\) \{
    this.$name = $name;
  \}
  public $type get$name\(\) \{
    return $name;
  \}
"
}

proc getTable {className id pairs} {
  append returnValue "
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;

  @Entity
  public class $className \{

  @Id
  @GeneratedValue\(strategy = GenerationType.IDENTITY\)
  private Long $id;\n"

  foreach {type name} $pairs {
    append returnValue "  private $type $name;\n"
  }

  append returnValue "
  public $className\(\) \{
  \}\n"

  foreach {type name} $pairs {
    append returnValue [getSetter $type $name]
  }

  append returnValue "
  @Override
  public String toString\(\) \{
    StringBuilder builder = new StringBuilder\(\);
    builder.append\(\"$className \[\"\);\n"

  set last [llength $pairs]
  foreach {type name} $pairs {
    append returnValue "    builder.append\(\"$name=\"\);\n"
    append returnValue "    builder.append\($name\);\n"
    if !$last {
      append returnValue "    builder.append\(\", \"\);\n"
    }
    set last [expr $last - 2]
  }
  set returnValue [string trim $returnValue "\n"]

  append returnValue "
    builder.append\(\"\]\"\);
    return builder.toString\(\);
  \}
\}"

  puts $returnValue
}

